﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarInfor.DAL;

namespace CarInfor.UI
{
    public partial class ctlInsuranceAlert : UserControl
    {
        DateTimePicker dtpStart = null;
        DateTimePicker dtpEnd = null;

        DateTimePicker dtpBussStart = null;
        DateTimePicker dtpBussEnd = null;
        string curConSql = string.Empty;
        DataSet curDataSet = new DataSet();
        public ctlInsuranceAlert()
        {
            InitializeComponent();
            Init();
        }

        /// <summary>
        /// 刷新界面
        /// </summary>
        public void RefreshUI()
        {
            AlertSetting ast = new AlertSetting();
            ast.GetSetting();
            this.dtpStart.Value = DateTime.Now.AddDays(ast.InsurancJQAlertB * (-1));
            this.dtpEnd.Value = DateTime.Now.AddDays(ast.InsurancJQAlertE);
            this.dtpBussStart.Value = DateTime.Now.AddDays(ast.InsurancBussAlertB * (-1));
            this.dtpBussEnd.Value = DateTime.Now.AddDays(ast.InsurancBussAlertE);

            if (!string.IsNullOrEmpty(this.dtpStart.Text) && !string.IsNullOrEmpty(this.dtpEnd.Text))
            {
                string sqlstr = " EndDate between #" + this.dtpStart.Text + "# and  #" + this.dtpEnd.Text + "#";
                curConSql = sqlstr;
                Bind(1, false);
            }
            else
            {
                curConSql = string.Empty;
                Bind(1, false);
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        void Init()
        {
            Common.SetGridViewStyle(this.dataGridView1);

            dtpStart = new DateTimePicker();
            dtpStart.Format = DateTimePickerFormat.Short;
            dtpStart.Width = 100;
            dtpEnd = new DateTimePicker();
            dtpEnd.Format = DateTimePickerFormat.Short;
            dtpEnd.Width = 100;
            this.toolStrip1.Items.Insert(3, new ToolStripControlHost(dtpStart));
            ToolStripLabel lbl = new ToolStripLabel();
            lbl.Text = "到";
            this.toolStrip1.Items.Insert(4, lbl);
            this.toolStrip1.Items.Insert(5, new ToolStripControlHost(dtpEnd));

            ToolStripLabel lblBuss = new ToolStripLabel();
            lblBuss.Text = "       商业险到期日期：";
            dtpBussStart = new DateTimePicker();
            dtpBussStart.Format = DateTimePickerFormat.Short;
            dtpBussStart.Width = 100;
            dtpBussEnd = new DateTimePicker();
            dtpBussEnd.Width = 100;
            dtpBussEnd.Format = DateTimePickerFormat.Short;

            this.toolStrip1.Items.Insert(10, lblBuss);
            this.toolStrip1.Items.Insert(11, (new ToolStripControlHost(dtpBussStart)));
            ToolStripLabel lbl2 = new ToolStripLabel();
            lbl2.Text = "到";
            this.toolStrip1.Items.Insert(12, lbl2);
            this.toolStrip1.Items.Insert(13, (new ToolStripControlHost(dtpBussEnd)));

            this.ctlPager1.PageSize = MyGlobal.PageSize;
        }

        private void Bind(int pageIndex, bool isPageEvent)
        {
            InsuranceForm insuranceForm = new InsuranceForm();
            DataTable dt = insuranceForm.Query(this.curConSql);

            //计算分页信息
            int startPos = (pageIndex - 1) * this.ctlPager1.PageSize;
            int count = this.ctlPager1.PageSize;
            if ((pageIndex - 1) * ctlPager1.PageSize + ctlPager1.PageSize > dt.Rows.Count)
            {
                count = dt.Rows.Count - startPos;
            }
            int total = dt.Rows.Count;

            //筛选分页数据
            Common.GetRange(dt, startPos, count);
            if (!isPageEvent)
            {
                this.ctlPager1.Initialize(total);
            }
            this.dataGridView1.DataSource = dt;
            curDataSet.Tables.Clear();
            curDataSet.Tables.Add(dt.Copy());

            this.dataGridView1.Columns[0].Width = 60;

            this.dataGridView1.Columns["编号"].Visible = false;
            this.dataGridView1.Columns["序号"].Width = 60;
            this.dataGridView1.Columns["交强险保单号"].Width = 110;
            this.dataGridView1.Columns["商业险保单号"].Width = 110;
            this.dataGridView1.Columns["被保险人"].Width = 100;
            this.dataGridView1.Columns["交强险参保日期"].Width = 120;
            this.dataGridView1.Columns["交强险到期日期"].Width = 120;
            this.dataGridView1.Columns["商业险参保日期"].Width = 120;
            this.dataGridView1.Columns["商业险到期日期"].Width = 120;
            this.dataGridView1.Columns["联系电话"].Width = 100;
            this.dataGridView1.Columns["车辆号牌"].Width = 100;
            this.dataGridView1.Columns["车辆类型"].Width = 100;
            this.dataGridView1.Columns["车架号"].Width = 100;
            this.dataGridView1.Columns["金额"].Width = 80;

            this.dataGridView1.Columns["交强险保单号"].ReadOnly = true;
            this.dataGridView1.Columns["商业险保单号"].ReadOnly = true;
            this.dataGridView1.Columns["被保险人"].ReadOnly = true;
            this.dataGridView1.Columns["交强险参保日期"].ReadOnly = true;
            this.dataGridView1.Columns["交强险到期日期"].ReadOnly = true;
            this.dataGridView1.Columns["商业险参保日期"].ReadOnly = true;
            this.dataGridView1.Columns["商业险到期日期"].ReadOnly = true;
            this.dataGridView1.Columns["联系电话"].ReadOnly = true;
            this.dataGridView1.Columns["车辆号牌"].ReadOnly = true;
            this.dataGridView1.Columns["车辆类型"].ReadOnly = true;
            this.dataGridView1.Columns["车架号"].ReadOnly = true;
            this.dataGridView1.Columns["金额"].ReadOnly = true;

            this.dataGridView1.Columns["交强险参保日期"].DefaultCellStyle.Format = "yyyy-MM-dd";
            this.dataGridView1.Columns["交强险到期日期"].DefaultCellStyle.Format = "yyyy-MM-dd";
            this.dataGridView1.Columns["商业险参保日期"].DefaultCellStyle.Format = "yyyy-MM-dd";
            this.dataGridView1.Columns["商业险到期日期"].DefaultCellStyle.Format = "yyyy-MM-dd";
            this.dataGridView1.Columns["报户日期"].DefaultCellStyle.Format = "yyyy-MM-dd";

            this.dataGridView1.Columns["核定"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.dataGridView1.Columns["单位"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.dataGridView1.Columns["金额"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.dataGridView1.Columns["金额"].DefaultCellStyle.Format = "0.00";

            this.chkSelectAll.Checked = false;

        }

        private void ctlInsurance_Load(object sender, EventArgs e)
        {
            AlertSetting ast = new AlertSetting();
            ast.GetSetting();
            this.dtpStart.Value = DateTime.Now.AddDays(ast.InsurancJQAlertB * (-1));
            this.dtpEnd.Value = DateTime.Now.AddDays(ast.InsurancJQAlertE);
            this.dtpBussStart.Value = DateTime.Now.AddDays(ast.InsurancBussAlertB * (-1));
            this.dtpBussEnd.Value = DateTime.Now.AddDays(ast.InsurancBussAlertE);

            if (!string.IsNullOrEmpty(this.dtpStart.Text) && !string.IsNullOrEmpty(this.dtpEnd.Text))
            {
                string sqlstr = " EndDate between #" + this.dtpStart.Text + "# and  #" + this.dtpEnd.Text + "#";
                curConSql = sqlstr;
                Bind(1, false);
            }
            else
            {
                this.curConSql = string.Empty;
                Bind(1, false);
            }
        }

        /// <summary>
        /// 商业险到期日期查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tssbtnQuery_ButtonClick(object sender, EventArgs e)
        {
            string sqlstr = " BussEndDate between #" + this.dtpStart.Text + "# and  #" + this.dtpEnd.Text + "#";
            curConSql = sqlstr;
            Bind(1, false);
        }

        /// <summary>
        /// 交强险到期日期查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnQueryForAlert_Click(object sender, EventArgs e)
        {
            string sqlstr = " EndDate between #" + this.dtpStart.Text + "# and  #" + this.dtpEnd.Text + "#";
            curConSql = sqlstr;
            Bind(1, false);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnEdit_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count != 0)
            {
                string id = this.dataGridView1.SelectedRows[0].Cells["编号"].Value.ToString();
                frmInsurance fi = new frmInsurance(ActionType.Edit, id);
                if (fi.ShowDialog() == DialogResult.OK)
                {
                    string sqlstr = " EndDate between #" + this.dtpStart.Text + "# and  #" + this.dtpEnd.Text + "#";
                    curConSql = sqlstr;
                    Bind(1, false);
                }
            }
        }

        /// <summary>
        /// 列表双击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > 0)
            {
                if (this.dataGridView1.SelectedRows.Count != 0)
                {
                    string id = this.dataGridView1.SelectedRows[0].Cells["编号"].Value.ToString();
                    frmInsurance fi = new frmInsurance(ActionType.Edit, id);
                    if (fi.ShowDialog() == DialogResult.OK)
                    {
                        string sqlstr = " EndDate between #" + this.dtpStart.Text + "# and  #" + this.dtpEnd.Text + "#";
                        curConSql = sqlstr;
                        Bind(1, false);
                    }
                }
            }
        }

        /// <summary>
        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkSelectAll.Checked)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    dataGridView1.EndEdit();
                    DataGridViewCheckBoxCell checkEditCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["chkSelect"];
                    checkEditCell.Value = true;
                }
            }
            else
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    dataGridView1.EndEdit();
                    DataGridViewCheckBoxCell checkEditCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["chkSelect"];
                    checkEditCell.Value = false;
                }
            }
        }

        /// <summary>
        /// 报表打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnPrint_Click(object sender, EventArgs e)
        {
            RDLCPrint.ReportViewer frmRPT = new RDLCPrint.ReportViewer();
            frmRPT.MainDataSet = curDataSet;
            frmRPT.ReportName = "Test";
            frmRPT.MainDataSourceName = "dbDataSet_InsuranceFormForReport";
            frmRPT.ReportPath = System.Windows.Forms.Application.StartupPath + @"\Report\Insurance.rdlc";
            frmRPT.ShowDialog();
        }

        /// <summary>
        /// 获取用户选择的数据
        /// </summary>
        /// <returns></returns>
        private string GetSelectRecord()
        {
            string ids = string.Empty;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                dataGridView1.EndEdit();
                DataGridViewCheckBoxCell checkEditCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["chkSelect"];
                Boolean flag = Convert.ToBoolean(checkEditCell.Value);
                if (flag)
                {
                    ids += "'" + this.dataGridView1.Rows[i].Cells["编号"].Value.ToString() + "',";
                }
            }
            return ids.TrimEnd(',');
        }

        private void btnSendMsg_Click(object sender, EventArgs e)
        {
            string rids = GetSelectRecord();
            if (string.IsNullOrEmpty(rids))
            {
                MessageHelper.ShowInfo("请选择要发送短信的客户！");
                return;
            }

            frmSendMsg msg = new frmSendMsg(MsgType.Insurance, rids);
            msg.ShowDialog();
        }

        private void ctlPager1_PageChanged(object sender, Controls.PagerArgs args)
        {
            Bind(args.PageIndex, true);
        }
    }
}